import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
import { BaseHttpService } from '../../../shared/services/baseHttp.service';
import { Observable } from 'rxjs/Observable';
import { CorporateGovernanceFilterModel } from './models/corporateGovernanceFilter.model';
import { CorporateGovernanceCompanyModel } from './models/corporateGovernanceCompany.model';

/**
 * Corporate Governance main service
 * 
 * Author: Andrei Istrate
 * Date: 30.06.2018
 */
@Injectable()
export class ResearchCorporateGovernanceService extends BaseHttpService {
    private serviceLink: string = "governanceserv/api/v1/companies?companyName=";
    constructor(private http: HttpClient) {
        super();
    }    

    public getCompanies(filters: CorporateGovernanceFilterModel, skip: number, take: number, orderBy: string, orderByAsc: boolean): Observable<CorporateGovernanceCompanyModel[]> {
        let params: HttpParams = new HttpParams();
        params = params.set("skip", skip.toString())
            .set("take", take.toString())
            .set('getCount', 'false')
            .set('orderBy', orderBy)
            .set('orderAscending', orderByAsc.toString());
        params = this.urlEncodeObject(filters, params, 'companyName');
        //because of the HppClient issue with '+' and probably other special chars, we set the parameter into the url
        return this.http.get<CorporateGovernanceCompanyModel[]>(this.serverUrl + this.serviceLink + this.paramEncode(filters, 'companyName'), { params: params });
    }

    public getCompaniesCount(filters: CorporateGovernanceFilterModel): any {
        let params: HttpParams = new HttpParams();
        params = params.set("getCount", 'true');
        params = this.urlEncodeObject(filters, params, 'companyName');
        //because of the HppClient issue with '+' and probably other special chars, we set the parameter into the url
        return this.http.get<any>(this.serverUrl + this.serviceLink + this.paramEncode(filters, 'companyName'), { params: params });
    }

}